package repository

import (
	"gorm.io/gorm"
	"shop-bean/model"
)

type IPictureRepository interface {
	Create(*gorm.DB, *model.Attachment) error
	FindInSimpleColumn(db *gorm.DB, ids []uint) ([]string, error)
	FindInFullColumn(db *gorm.DB, ids []uint) ([]*model.Attachment, error)
	UpdateBatchStatus(*gorm.DB, []uint, int) error
	DeleteBatch(*gorm.DB, []uint) error
}

type PictureRepository struct{}

func NewPictureRepository() IPictureRepository {
	return &PictureRepository{}
}

func (r *PictureRepository) Create(db *gorm.DB, file *model.Attachment) error {
	return db.Create(&file).Error
}

func (r *PictureRepository) FindInSimpleColumn(db *gorm.DB, ids []uint) (fileNames []string, err error) {
	return fileNames, db.Select("`name`").Model(&model.Attachment{}).
		Where("att_id in", ids).Where("status = 3").Find(&fileNames, ids).Error
}

func (r *PictureRepository) FindInFullColumn(db *gorm.DB, ids []uint) (rows []*model.Attachment, err error) {
	return rows, db.Model(&model.Attachment{}).Where("status = 3").Where("att_id in ?", ids).Find(&rows, ids).Error
}

func (r *PictureRepository) UpdateBatchStatus(db *gorm.DB, files []uint, state int) error {
	return db.Model(&model.Attachment{}).Where("att_id in", files).Update("status", state).Error
}

func (r *PictureRepository) DeleteBatch(db *gorm.DB, attIds []uint) error {
	return db.Where("att_id in ?", attIds).Delete(&model.Attachment{}).Error
}
